package service

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"gorm.io/gorm"
	"pet-service/basic/config"
	__ "pet-service/basic/proto"
	"pet-service/handler/model"
	"pet-service/pkg"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedPetServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) PetShopList(_ context.Context, in *__.PetShopListReq) (*__.PetShopListResp, error) {
	var shop []model.Shop
	var shoplist []*__.PetShopList
	config.DB.Find(&shop)
	for _, m := range shop {
		list := __.PetShopList{
			Title:   m.Title,
			Address: m.Address,
			Image:   m.Image,
			Type:    m.Type,
			Price:   m.Price,
			Stock:   m.Stock,
			Phone:   m.Phone,
		}
		shoplist = append(shoplist, &list)
	}
	return &__.PetShopListResp{
		List: shoplist,
	}, nil
}
func (s *Server) PetShopDetail(_ context.Context, in *__.PetShopDetailReq) (*__.PetShopDetailResp, error) {
	var shop model.Shop
	err := config.DB.Where("id=?", in.Id).First(&shop).Error
	if err != nil {
		panic("萌宠店铺详情失败")
	}
	return &__.PetShopDetailResp{
		Title:   shop.Title,
		Address: shop.Address,
		Image:   shop.Image,
		Type:    shop.Type,
		Price:   shop.Price,
		Stock:   shop.Stock,
		Phone:   shop.Phone,
	}, nil
}
func (s *Server) AppointmentCreate(_ context.Context, in *__.AppointmentCreateReq) (*__.AppointmentCreateResp, error) {
	appointment := model.Appointment{
		Model:   gorm.Model{},
		UserId:  in.UserId,
		ShopId:  in.ShopId,
		PetName: in.PetName,
		Price:   in.Price,
		Phone:   in.Phone,
		Context: in.Context,
		Status:  0,
	}
	err := config.DB.Create(&appointment).Error
	if err != nil {
		panic("预约失败")
	}
	return &__.AppointmentCreateResp{
		Id: int64(appointment.ID),
	}, nil
}
func (s *Server) OrderCreate(_ context.Context, in *__.OrderCreateReq) (*__.OrderCreateResp, error) {
	var user model.User
	err := config.DB.Where("id=?", in.UserId).Find(&user).Error
	if err != nil {
		return nil, fmt.Errorf("用户不存在")
	}
	var shop model.Shop
	err = config.DB.Where("id=?", in.ShopId).Find(&shop).Error
	if err != nil {
		return nil, fmt.Errorf("商店不存在")
	}
	order := model.Order{
		Model:   gorm.Model{},
		UserId:  in.UserId,
		ShopId:  in.ShopId,
		PetName: shop.Title,
		Price:   float32(in.Stock) * shop.Price,
		Phone:   shop.Phone,
		Context: shop.Context,
		PayType: 0,
		Status:  0,
	}
	err = config.DB.Create(&order).Error
	if err != nil {
		return nil, fmt.Errorf("创建失败")
	}
	Amount := fmt.Sprintf("%.2f", order.Price)
	alipay := pkg.Alipay(uuid.NewString(), Amount)
	return &__.OrderCreateResp{
		Url: alipay,
	}, nil
}
func (s *Server) OrderList(_ context.Context, in *__.OrderListReq) (*__.OrderListResp, error) {
	var orders []model.Order
	config.DB.Find(&orders)
	var orderlists []*__.OrderList
	for _, m := range orders {
		list := __.OrderList{
			UserId:  m.UserId,
			ShopId:  m.ShopId,
			PetName: m.PetName,
			Price:   m.Price,
			Phone:   m.Phone,
			Context: m.Context,
			PayType: m.PayType,
		}
		orderlists = append(orderlists, &list)
	}
	return &__.OrderListResp{
		List: orderlists,
	}, nil
}
